rm(list=ls(all=TRUE))  # new
library(foreign)
library(pscl)

load("rc.Rdata")
load("results.Rdata")
ideal.est = results[[1]]


### make a matrix with information on each legislator

trim <- function (x) gsub("^\\s+|\\s+$", "", x) # returns string w/o leading or trailing whitespace

make.members.matrix <- function(){
	members1 = colnames(ideal.est$x)
	members2 = strsplit(members1,split="[.]")
	for (i in 1:length(members2)){
		m = members2[[i]]
		mname = trim(paste(m[5:length(m)],collapse="."))
		members2[[i]] = c(m[1:4],mname)
	}
	t(matrix(unlist(members2),nrow=5))
}
members = make.members.matrix()
colnames(members)=c("type","cong","id","party","name")

### positions of members satisfying the criteria
get.pos <- function(type="",cong="",id="",party=""){
	cong1 = format(cong)
	id1 = format(id)
	pos1 = (members[,"type"] %in% type) | type==""
	pos2 = (members[,"cong"] %in% cong1) | cong==""
	pos3 = (members[,"id"] %in% id1) | id==""
	pos4 = (members[,"party"] %in% party) | party==""

	which(pos1&pos2&pos3&pos4)
}

### simulated ideal points of members satisfying the criteria
get.x <- function(type="",cong="",id="",party="",print.members=TRUE,save.att=FALSE){
	pos = get.pos(type,cong,id,party)
	att = members[pos,]
	if (print.members) {
		if (length(pos)==1) {
			print(cbind(pos,t(att)))
		} else { 
			print(cbind(pos,att))
		}
	}
	if (save.att){
		if (length(pos)==1){
			return(rbind(cong = att["cong"],type = att["type"],
			 party=att["party"],ideal = ideal.est$x[,pos,1]))

		} else {		
		return(rbind(cong = att[,"cong"],type = att[,"type"],
		 party=att[,"party"],ideal = ideal.est$x[,pos,1]))
		}
	} else{
		return(ideal.est$x[,pos,1])
	}
}


################################################################
#x1=get.x(id="29520")
#x105=get.x(id="29520",cong=105)
#x106=get.x(id="29520",cong=106)
#xHR103=get.x(cong=103,party="R",type="H")
#xP=get.x(type="P")
#x105_106 = get.x(id="29520", cong=c(105,106))
########################################################################


########################################
# Create matrices with members' ideal points for each congress
#
########################################
#common.mat

com.dw = read.dta(file="commonspaceDWNOMINATE.DTA")

# col1: congress num
# col2: id 
# col4: Congressional District Number (0 if Senate or President)
# col6: party (100=D, 200=R)
# col8: 1st dim coordinate

subset = com.dw [com.dw[,1]>100 ,]
subset = subset [subset[,1]<113 ,]


dw.id.table = rownames(table(subset[,2]))

dw.mat = matrix(NA, nrow = length(dw.id.table), ncol = 14)
rownames(dw.mat) = dw.id.table
colnames(dw.mat) = c( 101:112, "party","type")

for (j in 1:length(dw.id.table)){
	for (i in 1:12){
		id.pos = subset[,2]%in%dw.id.table[j]
		pos.list = which(id.pos)
		
		for (k in 1:length(pos.list)){
	#browser()	
		cong = subset[pos.list[k],1]
		
		dw.mat [j,(cong-100)]= subset[pos.list[k],8]
		dw.mat [j,"type"]= subset[pos.list[k],4]
		dw.mat [j,"party"]= subset[pos.list[k],6]
		
		}
	}
}


# change.mat
id.table = rownames( table(members[,"id"]) )

###################################################################
## change.mat is a matrix with posterior medians. 
## Each column from the third is for each congress, and each row is for each id. 
## For example, change.mat[1,3] shows the posterior median of 
#the first legislator in the 101st congress.
###################################################################
change.mat = matrix(NA, nrow = length(dw.id.table), ncol = 14)
rownames(change.mat)=dw.id.table
colnames(change.mat)=c("type","party",101:112)


for (i in 1:length(dw.id.table)){
	temp = get.x(id=dw.id.table[i],save.att=TRUE, print.members=FALSE)
	n.cong = dim(temp)[2]
	for(j in 1:n.cong){
		congr = as.numeric(temp["cong",j])-100
		change.mat[i,1]=temp["type",j]
		change.mat[i,2]=temp["party",j]
		change.mat[i,(congr+2)] = median(as.numeric(temp[c(-1,-2,-3),j]),na.rm = TRUE)	
	}
}	


#numbers only
dw.mat.num = dw.mat[,c(-13,-14)]
change.mat.num = change.mat[,c(-1,-2)]

dw.mat.num = (dw.mat.num-mean(as.vector(dw.mat.num), na.rm=TRUE))/sd(as.vector(dw.mat.num), na.rm=TRUE)

### 
h.dem.vec = (change.mat[,"type"]=="H" & change.mat[,"party"]=="D")
h.rep.vec = (change.mat[,"type"]=="H" & change.mat[,"party"]=="R")
s.dem.vec = (change.mat[,"type"]=="S" & change.mat[,"party"]=="D")
s.rep.vec = (change.mat[,"type"]=="S" & change.mat[,"party"]=="R")


# dw_nominate
h.dem.med.dw = rep(NA, times=12)
h.rep.med.dw = rep(NA, times=12)
s.dem.med.dw = rep(NA, times=12)
s.rep.med.dw = rep(NA, times=12)

for (i in 1:length(h.dem.med.dw)){

	h.dem.med.dw[i] = median(dw.mat[h.dem.vec,i],na.rm=TRUE)
	h.rep.med.dw[i] = median(dw.mat[h.rep.vec,i],na.rm=TRUE)
	s.dem.med.dw[i] = median(dw.mat[s.dem.vec,i],na.rm=TRUE)
	s.rep.med.dw[i] = median(dw.mat[s.rep.vec,i],na.rm=TRUE)
}


h.diff.dw = h.rep.med.dw - h.dem.med.dw 
s.diff.dw = s.rep.med.dw - s.dem.med.dw 

#ours
h.dem.med = rep(NA, times=12)
h.rep.med = rep(NA, times=12)
s.dem.med = rep(NA, times=12)
s.rep.med = rep(NA, times=12)

for (i in 1:length(h.dem.med)){

	h.dem.med[i] = median(as.numeric(change.mat.num[h.dem.vec,i]),na.rm=TRUE)
	h.rep.med[i] = median(as.numeric(change.mat.num[h.rep.vec,i]),na.rm=TRUE)
	s.dem.med[i] = median(as.numeric(change.mat.num[s.dem.vec,i]),na.rm=TRUE)
	s.rep.med[i] = median(as.numeric(change.mat.num[s.rep.vec,i]),na.rm=TRUE)
}

h.diff = h.rep.med - h.dem.med 
s.diff = s.rep.med - s.dem.med 


#### NORMALIZED differences in the party medians

h.diff.dw.n = (1/s.diff.dw[2])*h.diff.dw
s.diff.dw.n = (1/s.diff.dw[2])*s.diff.dw

h.diff.n = (1/s.diff[2])*h.diff
s.diff.n = (1/s.diff[2])*s.diff


#########################
#
# FIGURE 6
# (both in a single plot)
#
###########################

plot(x=102:111,y=h.diff.dw.n[2:11], type="b",xlab="Congress",ylim=c(0.8,2.2),ylab="",lwd=2,
 col=8)
lines(x=102:111,y=s.diff.dw.n[2:11],col=1,lwd=2,type="b")
lines(x=102:111,y=h.diff.n[2:11], col=8,lwd=2)
lines(x=102:111,y=s.diff.n[2:11],col=1,lwd=2)
legend("topleft", legend=c("House (Common Space DW-Nominate)","Senate (Common Space DW-Nominate)",
 "House ('Anchors Away' estimate)", "Senate ('Anchors Away' estimate)"),
 bty="n",col=c(8,1,8,1),lwd=2, pch=c(1,1,NA,NA))


